<script setup lang="ts">

import {useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import myAxios from "../../plugins/myAxios.ts";
import PostCardList from "../../components/PostCardList.vue";
import {Toast} from "vant";

const router = useRouter();
/*const searchText = ref('');*/

// 跳转到创建帖子页
const toAddPost = () => {
  router.push({
    path: "/post/add"
  })
}

const postList = ref([]);

/**
 * 搜索
 * @param val
 * @returns {Promise<void>}
 */
const listPost = async () => {
  const res = await myAxios.get("/post/list", {

  });

  if (res?.code === 0) {
    postList.value = res.data;
  } else {
    Toast.fail('获取失败，请刷新重试');
  }
}

/**
 * 条件搜索
 * @param val
 * @returns {Promise<void>}
const pagePost = async (val = '') => {
  const res = await myAxios.get("/post/page", {
    params: {
      searchText: val,
      pageNum: 1,
    },
  });
  if (res?.code === 0) {
    postList.value = res.data;
  } else {
    Toast.fail('获取失败，请刷新重试');
  }
}*/

// 页面加载时只触发一次
onMounted( () => {
  listPost();
})

/*const onSearch = (val) => {
  pagePost(val);
};*/


</script>


<template>
  <div id="postPage">
<!--
    <van-search v-model="searchText" placeholder="搜索标题" @search="onSearch" />
-->

    <post-card-list :postList="postList" />

    <van-empty v-if="postList?.length < 1" description="数据为空"/>
    <van-button class="add-button" type="primary" icon="plus" @click="toAddPost" />
  </div>
</template>



<style scoped>
</style>
